<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

// 引入Element Plus图标
import { User, Lock } from '@element-plus/icons-vue'

// 获取用户存储和路由
const userStore = useUserStore()
const router = useRouter()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

// 表单引用
const formRef = ref()

// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = async () => {
  if (!formRef.value) return

  try {
    // 表单验证
    await formRef.value.validate()

    // 设置加载状态
    loading.value = true

    // 调用登录方法
    const success = userStore.login(loginForm.username, loginForm.password)

    if (success) {
      ElMessage.success('登录成功')

      // 根据角色跳转到不同的页面
      if (userStore.userInfo?.role === 'admin') {
        router.push('/admin/bicycles')
      } else {
        router.push('/student/borrow')
      }
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <svg class="bicycle-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
            <path d="M5,20.5A3.5,3.5 0 0,1 1.5,17A3.5,3.5 0 0,1 5,13.5A3.5,3.5 0 0,1 8.5,17A3.5,3.5 0 0,1 5,20.5M5,12A5,5 0 0,0 0,17A5,5 0 0,0 5,22A5,5 0 0,0 10,17A5,5 0 0,0 5,12M14.8,10H19V8.2H15.8L13.8,4.8C13.5,4.3 12.8,4 12,4C11.2,4 10.5,4.3 10.2,4.8L8.2,8.2H5V10H8.8L10.8,13.5C11.1,14 11.8,14.2 12.5,14.2C13.2,14.2 13.9,14 14.2,13.5L16.2,10H14.8M19,20.5A3.5,3.5 0 0,1 15.5,17A3.5,3.5 0 0,1 19,13.5A3.5,3.5 0 0,1 22.5,17A3.5,3.5 0 0,1 19,20.5M19,12A5,5 0 0,0 14,17A5,5 0 0,0 19,22A5,5 0 0,0 24,17A5,5 0 0,0 19,12M16,4.8C17,4.8 17.8,4 17.8,3C17.8,2 17,1.2 16,1.2C15,1.2 14.2,2 14.2,3C14.2,4 15,4.8 16,4.8Z" />
          </svg>
        </div>
        <h2>校园单车管理系统</h2>
      </div>

      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>

        <div class="login-tips">
          <p>管理员账号: admin / admin123</p>
          <p>学生账号: student / student123</p>
        </div>

        <div class="register-link">
          <span>还没有账号？</span>
          <el-button type="text" @click="router.push('/register')">立即注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

.bicycle-icon {
  width: 60px;
  height: 60px;
  fill: #409eff;
}

.login-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  border-radius: 4px;
}

.login-tips {
  margin-top: 20px;
  color: #909399;
  font-size: 13px;
  text-align: center;
}

.login-tips p {
  margin: 5px 0;
}

.register-link {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}
</style>
